﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace cadbom
{
    public partial class QueryMaterialProductForm : Form
    {
        private System.Windows.Forms.Timer timer;
        private int searchDelay = 13000; // 设置延时，单位毫秒
        NetApi netApi = new NetApi();
        List<BasMaterial> materialItems = new List<BasMaterial>();
        List<BasProduct> productItems = new List<BasProduct>();
        private System.Data.DataTable dtBandProduct = null;
        private System.Data.DataTable dtBandMaterial = null;
        private Action<BasMaterial> materialCallback;
        private Action<BasProduct> productCallback;
        private int currentPage = 1;
        public QueryMaterialProductForm(Action<BasProduct> productCallback,Action<BasMaterial> materialCallback)
        {
            this.productCallback = productCallback;
            this.materialCallback = materialCallback;
            //this.tabControl1.SelectedIndex = 1;
            //timer = new System.Windows.Forms.Timer();
            //timer.Interval = searchDelay;
            //timer.Tick += new EventHandler(Timer_Tick);
          

            InitializeComponent();
            this.tabControl1.SelectedTab = this.tabPage2;
        }
        public void initAddMaterialButton(bool isVisiable)
        {
            this.button4.Visible = isVisiable;
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            //timer.Stop();
            //// 在这里编写查找逻辑
            //PerformSearch(textBox1.Text);
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //timer.Stop();
            //timer.Start();
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            //timer.Stop();
            //// 在这里编写查找逻辑
            //PerformSearch(textBox1.Text);
        }
        private void QueryMaterialProductForm_Load(object sender, EventArgs e)
        {

        }
        private void PerformSearch(string searchText)
        {
            // 执行查找操作
            if (1==1)
            {
                if (this.tabControl1.SelectedTab != null)
                {
                    // 获取当前选中的 TabPage 的名称
                    string currentTabPageName = this.tabControl1.SelectedTab.Name;

                    // 根据名称判断当前是哪个 TabPage
                    if (currentTabPageName == "tabPage1")
                    {
                        netApi.getProductDataList(searchText, new Action<ResponResultA<BasProduct>>((rs) =>
                        {
                            BindingList<BasProduct> products = rs.result.records;
                            List<BasProduct> items = products.ToList();
                            this.label5.Text = rs.result.total.ToString();
                            this.label6.Text = rs.result.pages.ToString();
                            this.label7.Text = currentPage.ToString();

                            this.button2.Enabled = currentPage > 1;
                            this.button3.Enabled = currentPage < rs.result.pages;

                            dtBandProduct = new System.Data.DataTable();
                            this.productItems = items;
                            dtBandProduct.Columns.Add("code");
                            dtBandProduct.Columns.Add("nameDesc");
                            dtBandProduct.Columns.Add("model");
                            dtBandProduct.Columns.Add("description");
                            dtBandProduct.Columns.Add("version");
                            dtBandProduct.Columns.Add("typeName");
                            dtBandProduct.Columns.Add("unitId_dictText");
                            dtBandProduct.Columns.Add("sizeLength");
                            dtBandProduct.Columns.Add("sizeWidth");
                            dtBandProduct.Columns.Add("id");
                            foreach (BasProduct mpb in items)
                            {
                                DataRow dr = dtBandProduct.NewRow();
                                dr["code"] = mpb.code;
                                dr["nameDesc"] = mpb.name;
                                dr["model"] = mpb.drawingNo;
                                dr["description"] = mpb.description;
                                dr["version"] = mpb.productVer;
                                dr["typeName"] = "半成品";//mpb.custCode;
                                dr["unitId_dictText"] = mpb.unitId_dictText;
                                dr["sizeLength"] = mpb.sizeLength;
                                dr["sizeWidth"] = mpb.sizeWidth;
                                dr["id"] = mpb.id;
                                dtBandProduct.Rows.Add(dr);
                            }
                            this.dataGridView2.DataSource = dtBandProduct;
                            dataGridView2.Columns["code"].HeaderText = "产品编码";
                            dataGridView2.Columns["nameDesc"].HeaderText = "产品名称";
                            dataGridView2.Columns["model"].HeaderText = "产品图号";
                            dataGridView2.Columns["description"].HeaderText = "产品描述";
                            dataGridView2.Columns["version"].HeaderText = "版本";
                            dataGridView2.Columns["typeName"].HeaderText = "客户料号";
                            dataGridView2.Columns["unitId_dictText"].HeaderText = "单位";
                            dataGridView2.Columns["sizeLength"].HeaderText = "长";
                            dataGridView2.Columns["sizeWidth"].HeaderText = "宽";
                            this.dataGridView2.Columns["id"].Visible = false;
                            foreach (DataGridViewColumn item in dataGridView2.Columns) item.SortMode = DataGridViewColumnSortMode.NotSortable;

                        }), currentPage);
                    }
                    else if (currentTabPageName == "tabPage2")
                    {
                        netApi.getMaterialData(searchText, new Action<ResponResultA<BasMaterial>>((rs) =>
                        {
                            BindingList<BasMaterial> products = rs.result.records;
                            List<BasMaterial> items = products.ToList();
                            items.ForEach(item =>
                            {
                                if (item.nameDesc == null || item.nameDesc == "")
                                {
                                    if (item.realName != null && item.realName != "")
                                    {
                                        item.nameDesc = item.realName;
                                    }
                                }
                                
                            });
                            this.label5.Text = rs.result.total.ToString();
                            this.label6.Text = rs.result.pages.ToString();
                            this.label7.Text = currentPage.ToString();

                            this.button2.Enabled = currentPage > 1;
                            this.button3.Enabled = currentPage < rs.result.pages;
                            dtBandMaterial = new System.Data.DataTable();
                            this.materialItems = items;
                            dtBandMaterial.Columns.Add("code");
                            dtBandMaterial.Columns.Add("nameDesc");
                            dtBandMaterial.Columns.Add("model");
                            dtBandMaterial.Columns.Add("description");
                            dtBandMaterial.Columns.Add("version");
                            dtBandMaterial.Columns.Add("typeName");
                            dtBandMaterial.Columns.Add("unitId_dictText");
                            dtBandMaterial.Columns.Add("sizeLength");
                            dtBandMaterial.Columns.Add("sizeWidth");
                            dtBandMaterial.Columns.Add("id");
                            foreach (BasMaterial mpb in items)
                            {
                                DataRow dr = dtBandMaterial.NewRow();
                                dr["code"] = mpb.code;
                                dr["nameDesc"] = mpb.nameDesc;
                                dr["model"] = mpb.model;
                                dr["description"] = mpb.description;
                                dr["version"] = mpb.materialVer;
                                dr["typeName"] = mpb.typeName;
                                dr["unitId_dictText"] = mpb.unitId_dictText;
                                dr["sizeLength"] = mpb.sizeLength;
                                dr["sizeWidth"] = mpb.sizeWidth;
                                dr["id"] = mpb.id;
                                dtBandMaterial.Rows.Add(dr);
                            }
                            this.dataGridView1.DataSource = dtBandMaterial;
                            dataGridView1.Columns["code"].HeaderText = "物料编码";
                            dataGridView1.Columns["nameDesc"].HeaderText = "物料名称";
                            dataGridView1.Columns["model"].HeaderText = "物料规格";
                            dataGridView1.Columns["description"].HeaderText = "物料描述";
                            dataGridView1.Columns["version"].HeaderText = "版本";
                            dataGridView1.Columns["typeName"].HeaderText = "物料类型";
                            dataGridView1.Columns["unitId_dictText"].HeaderText = "单位";
                            dataGridView1.Columns["sizeLength"].HeaderText = "长";
                            dataGridView1.Columns["sizeWidth"].HeaderText = "宽";

                            dataGridView1.Columns["model"].Width = 200;
                            this.dataGridView1.Columns["id"].Visible = false;
                            foreach (DataGridViewColumn item in dataGridView1.Columns) item.SortMode = DataGridViewColumnSortMode.NotSortable;

                        }),currentPage);
                    }
                    // 可以根据需要添加更多的判断条件
                }
                else
                {
                    // 没有选中任何 TabPage
                    MessageBox.Show("没有选中任何 TabPage");
                }
            }

        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var indexRow = ((System.Windows.Forms.DataGridView)(sender)).CurrentRow.Index;
            var indexCol = ((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex;
            materialCallback.Invoke(this.materialItems[indexRow]);
        }

        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var indexRow = ((System.Windows.Forms.DataGridView)(sender)).CurrentRow.Index;
            var indexCol = ((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex;
            productCallback.Invoke(this.productItems[indexRow]);
        }

        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
                    using
            (SolidBrush b =
            new
            SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
            {

                string
                linenum = e.RowIndex.ToString();
                int
                linen = 0;
                linen = Convert.ToInt32(linenum) + 1;
                string
                line = linen.ToString();
                e.Graphics.DrawString(line, e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 5);
                SolidBrush B =
                new
                SolidBrush(Color.Red);
            }
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            using
                (SolidBrush b =
                new
                SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
            {

                string
                linenum = e.RowIndex.ToString();
                int
                linen = 0;
                linen = Convert.ToInt32(linenum) + 1;
                string
                line = linen.ToString();
                e.Graphics.DrawString(line, e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 5);
                SolidBrush B =
                new
                SolidBrush(Color.Red);
            }
        }

        private void tabControl1_Click(object sender, EventArgs e)
        {
            //PerformSearch(textBox1.Text);
        }

        private void tabControl1_TabIndexChanged(object sender, EventArgs e)
        {
            //PerformSearch(textBox1.Text);
        }

        private void QueryMaterialProductForm_Deactivate(object sender, EventArgs e)
        {
            //this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            currentPage = 1;
            PerformSearch(textBox1.Text);
        }
        //上一页
        private void button2_Click(object sender, EventArgs e)
        {
            if (currentPage > 1)
            {
                currentPage--;
                PerformSearch(textBox1.Text);
            }
        }
        //下一页
        private void button3_Click(object sender, EventArgs e)
        {
            currentPage++;
            PerformSearch(textBox1.Text);
        }
        //总记录数
        private void label5_Click(object sender, EventArgs e)
        {

        }
        //总页数
        private void label6_Click(object sender, EventArgs e)
        {

        }
        //当前页
        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            BasMaterialForm basMaterialForm = new BasMaterialForm(new Action<BasMaterial>(mat => {
                materialCallback.Invoke(mat);
            }));
            netApi.getMaterialCategory(new Action<ResponResultA<BasMaterialCategory>>((rs) =>
            {
                BindingList<BasMaterialCategory> products = rs.result.records;
                List<BasMaterialCategory> items = products.ToList();

                basMaterialForm.initMatCat(JsonConvert.SerializeObject(items));
                netApi.getBasMeasureUnit(new Action<ResponResultA<BasMeasureUnit>>((rs1) =>
                {
                    BindingList<BasMeasureUnit> units = rs1.result.records;
                    List<BasMeasureUnit> unitList = units.ToList();
                    basMaterialForm.initUnit(JsonConvert.SerializeObject(unitList));
                    Autodesk.AutoCAD.ApplicationServices.Application.ShowModelessDialog(null, basMaterialForm);
                }));

            }));


        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                currentPage = 1;
                PerformSearch(textBox1.Text);
            }
        }
    }
}
